package net.realsee.service.wcs.device.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.device.WcsVehicle;
import net.realsee.domain.wcs.device.bo.WcsVehicleBo;
import net.realsee.domain.wcs.device.vo.WcsVehicleVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.device.WcsVehicleMapper;
import net.realsee.service.wcs.device.IWcsVehicleService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 车辆Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsVehicleServiceImpl extends ServiceImpl<WcsVehicleMapper, WcsVehicle> implements IWcsVehicleService {

    private final WcsVehicleMapper baseMapper;

    /**
     * 查询车辆
     */
    @Override
    public WcsVehicleVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询车辆列表
     */
    @Override
    public TableDataInfo<WcsVehicleVo> selectVoPage(WcsVehicleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsVehicle> lqw = buildVoQueryWrapper(bo);
        Page<WcsVehicleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询车辆列表
     */
    @Override
    public List<WcsVehicleVo> selectVoList(WcsVehicleBo bo) {
        LambdaQueryWrapper<WcsVehicle> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsVehicle> buildVoQueryWrapper(WcsVehicleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsVehicle> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsVehicle::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleNo()), WcsVehicle::getVehicleNo, bo.getVehicleNo());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleType()), WcsVehicle::getVehicleType, bo.getVehicleType());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleId()), WcsVehicle::getVehicleId, bo.getVehicleId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WcsVehicle::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsVehicle::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsVehicle::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsVehicle::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsVehicle::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsVehicle::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsVehicle::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsVehicle::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsVehicle::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsVehicle::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsVehicle::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsVehicle::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsVehicle::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsVehicle::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增车辆
     */
    @Override
    public Boolean insertByBo(WcsVehicleBo bo) {
        WcsVehicle add = BeanUtil.toBean(bo, WcsVehicle.class);
        return this.save(add);
    }

    /**
     * 修改车辆
     */
    @Override
    public Boolean updateByBo(WcsVehicleBo bo) {
        WcsVehicle update = BeanUtil.toBean(bo, WcsVehicle.class);
        return this.updateById(update);
    }

    /**
     * 批量删除车辆
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
